package com.xjscrm.console.service.customer.operatePlan.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.github.pagehelper.PageHelper;
import com.uzai.mobile.collect.api.MicaTopics;
import com.uzai.mobile.collect.api.dto.MicaTriggerDto;
import com.uzai.mobile.collect.api.dto.msg.MicaFriendAddMsg;
import com.uzai.mobile.collect.api.enums.MicaTriggerType;
import com.xjscrm.common.constant.ResponseMsg;
import com.xjscrm.common.entity.*;
import com.xjscrm.common.enums.DeviceTypeEnum;
import com.xjscrm.common.enums.RedisCacheKeyEnum;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.common.utils.DateUtil;
import com.xjscrm.common.utils.OrderByUtil;
import com.xjscrm.common.vo.custmoerplan.CustomerPlanRule;
import com.xjscrm.common.vo.custmoerplan.TriggerCondition;
import com.xjscrm.common.vo.grouppull.GroupPullData;
import com.xjscrm.common.vo.grouppull.GroupRule;
import com.xjscrm.common.vo.grouppull.GroupRuleInfo;
import com.xjscrm.common.vo.newcustmoerplan.NewCustomerPlanRule;
import com.xjscrm.common.vo.operatorplandevice.OperatorPlanDeviceRule;
import com.xjscrm.console.mapper.*;
import com.xjscrm.console.mq.producer.SendMsgProducer;
import com.xjscrm.console.param.BaseDto;
import com.xjscrm.console.param.operatePlan.*;
import com.xjscrm.console.param.user.ChildMerchatDto;
import com.xjscrm.console.param.wwDevice.WwDeviceQuery;
import com.xjscrm.console.service.common.user.UserService;
import com.xjscrm.console.service.customer.childUser.ChildMerchatService;
import com.xjscrm.console.service.customer.operatePlan.OperatePlanService;
import com.xjscrm.console.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class OperatePlanServiceImpl implements OperatePlanService {

    @Autowired
    private OperatePlanMapper operatePlanMapper;

    @Autowired
    private OperatePlanDeviceMapper operatePlanDeviceMapper;

    @Autowired
    private WwFriendReqMapper wwFriendReqMapper;

    @Autowired
    private DeviceGroupRelationMapper deviceGroupRelationMapper;

    @Autowired
    private WwDeviceMapper wwDeviceMapper;

    @Autowired
    private SendMsgProducer sendMsgProducer;

    @Autowired
    private UserService userService;

    @Autowired
    private ChildMerchatService childMerchatService;

    @Autowired
    private RedisTemplate redisTemplate;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOperatePlan(OperatePlanDto operatePlanDto) {
       if(operatePlanDto.getMerId()==null){
           throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
       }
       if(operatePlanDto.getPlanType()==null){
           throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
       }
        if(operatePlanDto.getDeviceType()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(operatePlanDto.getRuleType()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(StringUtils.isBlank(operatePlanDto.getPlanName())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }

        //添加方案
        OperatePlan operatePlan = new OperatePlan();
        BeanUtil.copyProperties(operatePlanDto,operatePlan);
        long operatePlanId = IdWorker.getId();
        int nowTime = DateUtil.getTime();
        operatePlan.setId(operatePlanId);
        operatePlan.setCreateTime(nowTime);
        operatePlan.setUpdateTime(nowTime);

        //新客方案规则
        if(operatePlanDto.getPlanType()==0){
            if(operatePlanDto.getNewCustomerPlanRule()==null){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
            }
            operatePlan.setPlanRule(JSONObject.toJSONString(operatePlanDto.getNewCustomerPlanRule()));
        }

        //运营方案规则
        if(operatePlanDto.getPlanType()==1){
            if(operatePlanDto.getCustomerPlanRule()==null){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
            }
            operatePlan.setPlanRule(JSONObject.toJSONString(operatePlanDto.getCustomerPlanRule()));
        }

        operatePlanMapper.insertOperatePlan(operatePlan);
    }

    @Override
    public Object getOperatePlan(OperatePlanQuery operatePlanQuery) {
        if(operatePlanQuery.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        String orderBy = OrderByUtil.getOrderBy(operatePlanQuery.getOrderByField(), operatePlanQuery.isAsc());
        PageHelper.startPage(operatePlanQuery.getCurrent(),operatePlanQuery.getSize(),orderBy);
        List<OperatePlanVo>operatePlanList=operatePlanMapper.getOperatePlanList(operatePlanQuery);
        if(operatePlanList!=null&&!operatePlanList.isEmpty()){
            for (OperatePlanVo operatePlanVo:operatePlanList) {
                try {
                    //新客
                    if(operatePlanVo.getPlanType()==0){
                        operatePlanVo.setNewCustomerPlanRule(JSONObject.parseObject(operatePlanVo.getPlanRule(),NewCustomerPlanRule.class));
                    }
                    //运营
                    if(operatePlanVo.getPlanType()==1){
                        operatePlanVo.setCustomerPlanRule(JSONObject.parseObject(operatePlanVo.getPlanRule(), CustomerPlanRule.class));
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        MyPageInfo<OperatePlanVo> pageInfo=new MyPageInfo<>(operatePlanList);
        return pageInfo;
    }

    @Override
    public Object getOperatePlanDetails(OperatePlanDto operatePlanDto) {
        if(operatePlanDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        OperatePlan operatePlan=operatePlanMapper.getOperatePlanById(operatePlanDto.getId(),operatePlanDto.getMerId());
        OperatePlanVo operatePlanVo = new OperatePlanVo();
        BeanUtil.copyProperties(operatePlan,operatePlanVo);
        //新客
        if(operatePlanVo.getPlanType()==0){
            operatePlanVo.setNewCustomerPlanRule(JSONObject.parseObject(operatePlan.getPlanRule(), NewCustomerPlanRule.class));
        }
        //运营
        if(operatePlanVo.getPlanType()==1){
            operatePlanVo.setCustomerPlanRule(JSONObject.parseObject(operatePlan.getPlanRule(), CustomerPlanRule.class));
        }
        return operatePlanVo;
    }

    @Override
    public void updateOperatePlan(OperatePlanDto operatePlanDto) {
        if(operatePlanDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(operatePlanDto.getId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.ID_NULL);
        }
       OperatePlan operatePlan=operatePlanMapper.getOperatePlanById(operatePlanDto.getId(),operatePlanDto.getMerId());
        if(operatePlan==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PLAN_NULL);
        }
        BeanUtil.copyProperties(operatePlanDto,operatePlan);

        //新客方案规则
        if(operatePlanDto.getPlanType()==0){
            if(operatePlanDto.getNewCustomerPlanRule()==null){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
            }
            operatePlan.setPlanRule(JSONObject.toJSONString(operatePlanDto.getNewCustomerPlanRule()));
        }

        //运营方案规则
        if(operatePlanDto.getPlanType()==1){
            if(operatePlanDto.getCustomerPlanRule()==null){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
            }
            operatePlan.setPlanRule(JSONObject.toJSONString(operatePlanDto.getCustomerPlanRule()));
        }

        operatePlanMapper.updateOperatePlan(operatePlan);
    }

    @Override
    @Transactional
    public void deleOperatePlan(OperatePlanDto operatePlanDto) {
        if(operatePlanDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(operatePlanDto.getId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.ID_NULL);
        }
        //删除方案
        operatePlanMapper.deleOperatePlanById(operatePlanDto.getId(),operatePlanDto.getMerId());

        //删除方案和机器人关系
        OperatePlanDeviceQuery operatePlanDeviceQuery = new OperatePlanDeviceQuery();
        operatePlanDeviceQuery.setMerId(operatePlanDto.getMerId());
        operatePlanDeviceQuery.setPlanId(operatePlanDto.getId());
        List<OperatePlanDeviceVo> operatePlanDeviceList = operatePlanDeviceMapper.getOperatePlanDeviceList(operatePlanDeviceQuery);
        if(operatePlanDeviceList!=null){
            operatePlanDeviceList.forEach(it->{
                operatePlanDeviceMapper.deleOperatePlanDeviceById(it.getId(), operatePlanDeviceQuery.getMerId());
            });
        }
    }

    @Override
    public void addOperatePlanDevice(OperatePlanDeviceDto operatePlanDeviceDto) {
        if(operatePlanDeviceDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(operatePlanDeviceDto.getPlanType()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(operatePlanDeviceDto.getRuleType()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(operatePlanDeviceDto.getPlanId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(operatePlanDeviceDto.getPlanId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(operatePlanDeviceDto.getDeviceIds()==null|| operatePlanDeviceDto.getDeviceIds().isEmpty()){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }

        //新客方案:一个机器人只能有一个方案，查询这些机器人是否有其他接受新客方案
        if(operatePlanDeviceDto.getPlanType()==0){
            OperatePlanDeviceQuery operatePlanDeviceQuery=new OperatePlanDeviceQuery();
            operatePlanDeviceQuery.setPlanType(operatePlanDeviceDto.getPlanType());
            operatePlanDeviceQuery.setDeviceIds(operatePlanDeviceDto.getDeviceIds());
            operatePlanDeviceQuery.setMerId(operatePlanDeviceDto.getMerId());
            List<OperatePlanDeviceVo> operatePlanDeviceList= operatePlanDeviceMapper.getOperatePlanDeviceList(operatePlanDeviceQuery);
            if(operatePlanDeviceList!=null&& !operatePlanDeviceList.isEmpty()){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "一个机器人只能有一个接受新客方案");
            }
        }

        //运营方案 一个机器人可以有默认和按条件两种
        if(operatePlanDeviceDto.getPlanType()==1){
            OperatePlanDeviceQuery operatePlanDeviceQuery=new OperatePlanDeviceQuery();
            operatePlanDeviceQuery.setPlanType(operatePlanDeviceDto.getPlanType());
            operatePlanDeviceQuery.setDeviceIds(operatePlanDeviceDto.getDeviceIds());
            operatePlanDeviceQuery.setMerId(operatePlanDeviceDto.getMerId());
            List<OperatePlanDeviceVo> operatePlanDeviceList= operatePlanDeviceMapper.getOperatePlanDeviceList(operatePlanDeviceQuery);
            if(operatePlanDeviceList!=null&&!operatePlanDeviceList.isEmpty()){
                for (OperatePlanDeviceVo operatePlanDeviceVo:operatePlanDeviceList) {
                    if(Objects.equals(operatePlanDeviceDto.getRuleType(), operatePlanDeviceVo.getRuleType())){
                        throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "一个机器人只能有一个默认新客运营/一个按条件运营方案");
                    }
                }
            }
        }

        //添加方案与机器人关系
        for (Long deviceId:operatePlanDeviceDto.getDeviceIds()) {
            OperatePlanDevice operatePlanDevice=new OperatePlanDevice();
            operatePlanDevice.setDeviceUniqueId(deviceId);
            operatePlanDevice.setPlanType(operatePlanDeviceDto.getPlanType());
            operatePlanDevice.setRuleType(operatePlanDeviceDto.getRuleType());
            operatePlanDevice.setMerId(operatePlanDeviceDto.getMerId());
            operatePlanDevice.setPlanId(operatePlanDeviceDto.getPlanId());
            operatePlanDeviceMapper.addOperatePlanDevice(operatePlanDevice);
        }

    }

    @Override
    public void deleOperatePlanDevice(OperatePlanDeviceDto operatePlanDeviceDto) {
        if(operatePlanDeviceDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(operatePlanDeviceDto.getId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.ID_NULL);
        }
        operatePlanDeviceMapper.deleOperatePlanDeviceById(operatePlanDeviceDto.getId(),operatePlanDeviceDto.getMerId());
    }

    @Override
    public void bathdeleOperatePlanDevice(BathdeleOperatePlanDeviceDto bathdeleOperatePlanDeviceDto) {
        if(bathdeleOperatePlanDeviceDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(bathdeleOperatePlanDeviceDto.getIds()==null||bathdeleOperatePlanDeviceDto.getIds().isEmpty()){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.ID_NULL);
        }
        bathdeleOperatePlanDeviceDto.getIds().forEach(it->{
            operatePlanDeviceMapper.deleOperatePlanDeviceById(it,bathdeleOperatePlanDeviceDto.getMerId());
        });
    }

    @Override
    public Object getOperatePlanDevice(OperatePlanDeviceQuery operatePlanDeviceQuery) {
        if(operatePlanDeviceQuery.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        //获取对应分组的机器人
        List<Long>deviceIds=new ArrayList<>();
        if(operatePlanDeviceQuery.getGroupId()!=null){
            DeviceGroupRelationQuery deviceGroupRelationQuery = new DeviceGroupRelationQuery();
            deviceGroupRelationQuery.setGroupId(operatePlanDeviceQuery.getGroupId());
            deviceGroupRelationQuery.setMerId(operatePlanDeviceQuery.getMerId());
           List<DeviceGroupRelation> deviceGroupRelationList= deviceGroupRelationMapper.deviceGroupRelationList(deviceGroupRelationQuery);
           if(deviceGroupRelationList!=null&&!deviceGroupRelationList.isEmpty()){
               for (DeviceGroupRelation deviceGroupRelation:deviceGroupRelationList) {
                   try {
                       deviceIds.add(deviceGroupRelation.getDeviceUniqueId());
                   }catch (Exception e){
                       e.printStackTrace();
                   }
               }
           }
           if(deviceIds.isEmpty()){
               return new MyPageInfo<>(null);
           }
        }
        //通过关键词获取机器人ID
        List<Long> keywordDeviceIds=new ArrayList<>();
        if(!StringUtils.isBlank(operatePlanDeviceQuery.getKeyword())) {
            WwDeviceQuery wwDeviceQuery = new WwDeviceQuery();
            wwDeviceQuery.setKeyword(operatePlanDeviceQuery.getKeyword());
            wwDeviceQuery.setMerId(operatePlanDeviceQuery.getMerId());
            List<WwDeviceVo> wwDeviceList = wwDeviceMapper.getWwDeviceList(wwDeviceQuery);
            if (wwDeviceList != null && !wwDeviceList.isEmpty()) {
                keywordDeviceIds = wwDeviceList.stream().map(WwDeviceVo::getId).distinct().collect(Collectors.toList());
            }
            if(keywordDeviceIds.isEmpty()){
                return new MyPageInfo<>(null);
            }
        }

        if(!deviceIds.isEmpty()&&keywordDeviceIds.isEmpty()){
            operatePlanDeviceQuery.setDeviceIds(deviceIds);
        }
        if(deviceIds.isEmpty()&&!keywordDeviceIds.isEmpty()){
            operatePlanDeviceQuery.setDeviceIds(keywordDeviceIds);
        }
        if(!deviceIds.isEmpty()&&!keywordDeviceIds.isEmpty()){
            List<Long> intersection = deviceIds.stream().filter(keywordDeviceIds::contains).collect(Collectors.toList());
            operatePlanDeviceQuery.setDeviceIds(intersection);
        }
        //获取机器人方案列表
        String orderBy = OrderByUtil.getOrderBy(operatePlanDeviceQuery.getOrderByField(), operatePlanDeviceQuery.isAsc());
        PageHelper.startPage(operatePlanDeviceQuery.getCurrent(),operatePlanDeviceQuery.getSize(),orderBy);
        List<OperatePlanDeviceVo> operatePlanDeviceList = operatePlanDeviceMapper.getOperatePlanDeviceList(operatePlanDeviceQuery);

        //获取机器人信息
        for (OperatePlanDeviceVo operatePlanDeviceVo :operatePlanDeviceList) {
            try {
             WwDevice wwDevice= wwDeviceMapper.getById(operatePlanDeviceVo.getDeviceUniqueId(),operatePlanDeviceVo.getMerId());
                operatePlanDeviceVo.setDeviceName(wwDevice.getDeviceName());
                operatePlanDeviceVo.setHeadImgUrl(wwDevice.getHeadImgUrl());
                operatePlanDeviceVo.setLastLoginTime(wwDevice.getLastLoginTime());
            }catch (Exception e){
                e.printStackTrace();
            }

            try{
                OperatorPlanDeviceRule ruleObject = JSONObject.parseObject(operatePlanDeviceVo.getRule(), OperatorPlanDeviceRule.class);
                operatePlanDeviceVo.setRuleObject(ruleObject);
            }catch (Exception e){

            }

        }
        MyPageInfo<OperatePlanDeviceVo> pageInfo = new MyPageInfo<>(operatePlanDeviceList);
        return pageInfo;
    }

    /**
     * 修改新客户和机器人关系的应用列表
     * @author jitwxs
     * @date 2024/3/12 18:36
     * @param operatePlanDeviceDto 
     */
    public void updateOperatePlanDevice(OperatePlanDeviceDto operatePlanDeviceDto){
        if(operatePlanDeviceDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }

        if(operatePlanDeviceDto.getId() == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入主键id");
        }

        OperatePlanDevice operatePlanDevice = operatePlanDeviceMapper.getById(operatePlanDeviceDto.getId(), operatePlanDeviceDto.getMerId());

        if(operatePlanDevice == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该配置方案不存在");
        }

        operatePlanDevice.setRule(JSONObject.toJSONString(operatePlanDeviceDto.getRule()));
        operatePlanDevice.setUpdateTime(DateUtil.getTime());
        operatePlanDeviceMapper.update(operatePlanDevice);
    }

    public void batchUpdateOperatePlanDevice(OperatePlanDeviceDto operatePlanDeviceDto){
        if(operatePlanDeviceDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }

        if(operatePlanDeviceDto.getIdList() == null || operatePlanDeviceDto.getIdList().size() == 0){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入主键id列表");
        }

        for (Long id : operatePlanDeviceDto.getIdList()) {
            try {
                operatePlanDeviceDto.setId(id);
                updateOperatePlanDevice(operatePlanDeviceDto);
            }catch (Exception e){

            }
        }
    }

    @Override
    public Object getWwFriendReq(WwFriendReqQuery wwFriendReqQuery) {
        if(wwFriendReqQuery.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(wwFriendReqQuery.getStartTime()==null&&wwFriendReqQuery.getEndTime()==null){
            wwFriendReqQuery.setEndTime(DateUtil.getTime());
            wwFriendReqQuery.setStartTime(DateUtil.getAnyTime(-30,0,0,0));
        }
        String orderBy = OrderByUtil.getOrderBy(wwFriendReqQuery.getOrderByField(), wwFriendReqQuery.isAsc());
        PageHelper.startPage(wwFriendReqQuery.getCurrent(),wwFriendReqQuery.getSize(),orderBy);
        List<WwFriendReqVo>wwFriendReqVoList=wwFriendReqMapper.getWwFriendReq(wwFriendReqQuery);
        if(wwFriendReqVoList!=null&&!wwFriendReqVoList.isEmpty()){
            for (WwFriendReqVo wwFriendReqVo:wwFriendReqVoList) {
                try {
                    WwDevice wwDevice = wwDeviceMapper.getById(wwFriendReqVo.getDeviceUniqueId(), wwFriendReqVo.getMerId());
                    wwFriendReqVo.setDeviceHeadImage(wwDevice.getHeadImgUrl());
                    wwFriendReqVo.setDeviceNickName(wwDevice.getDeviceName());

                    //主账号操作
                    if(wwFriendReqVo.getOperateId()!=null&&wwFriendReqVo.getOperateType()!=null&&wwFriendReqVo.getOperateType()==0){
                        BaseDto baseDto=new BaseDto();
                        baseDto.setMerId(wwFriendReqVo.getOperateId());
                        Merchant merchant = userService.getById(baseDto);
                        wwFriendReqVo.setOperateName(merchant.getUserName());
                    }
                    //子账号操作
                    if(wwFriendReqVo.getOperateId()!=null&&wwFriendReqVo.getOperateType()!=null&&wwFriendReqVo.getOperateType()==1){
                        ChildMerchatDto childMerchatDto=new ChildMerchatDto();
                        childMerchatDto.setMerId(wwFriendReqQuery.getMerId());
                        childMerchatDto.setId(wwFriendReqVo.getOperateId());
                        MerchatChild merchatChild = childMerchatService.getById(childMerchatDto);
                        wwFriendReqVo.setOperateName(merchatChild.getUserName());
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

        MyPageInfo<WwFriendReqVo> pageInfo = new MyPageInfo<>(wwFriendReqVoList);
        return pageInfo;
    }

    @Override
    public void thoughFriends(WwFriendReqDto wwFriendReqDto) {
        if(wwFriendReqDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(wwFriendReqDto.getStatus()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(wwFriendReqDto.getOperateType()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
       WwFriendReq wwFriendReq= wwFriendReqMapper.getById(wwFriendReqDto.getId(),wwFriendReqDto.getMerId());
        if(wwFriendReq==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "新客方案请求记录不存在");
        }
        wwFriendReq.setStatus(wwFriendReqDto.getStatus());
        wwFriendReq.setOptTime(DateUtil.getTime());
        if(wwFriendReqDto.getOperateType()==0){
            wwFriendReq.setOperateId(wwFriendReqDto.getMerId());
            wwFriendReq.setOperateType(0);
        }
        if(wwFriendReqDto.getOperateType()==1){
            wwFriendReq.setOperateId(wwFriendReqDto.getChildId());
            wwFriendReq.setOperateType(1);
        }
        Integer update=wwFriendReqMapper.updateWwFriendReq(wwFriendReq);

        //是通过好友且修改数据库成功，往mq推送消息
     if(wwFriendReqDto.getStatus()==1&&update==1){
         MicaFriendAddMsg micaFriendAddMsg=new MicaFriendAddMsg();
         micaFriendAddMsg.setNickName(wwFriendReq.getNickName());
         micaFriendAddMsg.setWxid(wwFriendReq.getWxid());
         MicaTriggerDto micaTriggerDto= MicaTriggerDto.newInstance(micaFriendAddMsg, MicaTriggerType.TRIGGER_ACCEPT_FRIEND);
         micaTriggerDto.setMerId(wwFriendReq.getMerId());
         micaTriggerDto.setDeviceId(wwFriendReq.getDeviceId());
         micaTriggerDto.setDeviceType(DeviceTypeEnum.DT_ANDROID_WORK_WECHAT_VALUE);
         sendMsgProducer.pushMsg(micaTriggerDto, MicaTopics.WW_TRIGGER_TOPIC);
     }
    }

    @Override
    public boolean checkBandingDevice(OperatePlanDeviceDto operatePlanDeviceDto) {
        if(operatePlanDeviceDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.MERID_NULL);
        }
        if(operatePlanDeviceDto.getPlanId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.PARAMETER_ERROR);
        }
        OperatePlanDeviceQuery operatePlanDeviceQuery=new OperatePlanDeviceQuery();
        operatePlanDeviceQuery.setPlanId(operatePlanDeviceDto.getPlanId());
        operatePlanDeviceQuery.setMerId(operatePlanDeviceQuery.getMerId());
        List<OperatePlanDeviceVo> operatePlanDeviceList = operatePlanDeviceMapper.getOperatePlanDeviceList(operatePlanDeviceQuery);
        if(operatePlanDeviceList==null||operatePlanDeviceList.isEmpty()){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public Object findAddFangetEffectuateGroupGroup(EffectuateGroupDto effectuateGroupDto) {
        if(effectuateGroupDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(effectuateGroupDto.getPlanId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(StringUtils.isBlank(effectuateGroupDto.getGroupPullKey())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        OperatePlan operatePlan = operatePlanMapper.getOperatePlanById(effectuateGroupDto.getPlanId(), effectuateGroupDto.getMerId());
        if(operatePlan==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该方案不存在");
        }
        if(StringUtils.isBlank(operatePlan.getPlanRule())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该方案规则不存在");
        }
        CustomerPlanRule customerPlanRule = JSONObject.parseObject(operatePlan.getPlanRule(), CustomerPlanRule.class);
        if(customerPlanRule==null||customerPlanRule.getTriggerConditions()==null||customerPlanRule.getTriggerConditions().isEmpty()){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该方案规则不存在");
        }

        for (TriggerCondition triggerCondition:customerPlanRule.getTriggerConditions()) {
            if(triggerCondition.getMsgDatas()!=null&&!triggerCondition.getMsgDatas().isEmpty()){

                List<GroupPullData> groupPullDataList = triggerCondition.getMsgDatas();
                if(groupPullDataList==null||groupPullDataList.isEmpty()){
                 continue;
                }
                List<EffectuateGroupVo> effectuateGroupVoList=new ArrayList<>();

                for (GroupPullData groupPullData:groupPullDataList) {
                    if(groupPullData.getMsgType()!=null&&groupPullData.getMsgType().equals(100)){
                        if(!StringUtils.isBlank(groupPullData.getGroupPullKey())&&groupPullData.getGroupPullKey().equals(effectuateGroupDto.getGroupPullKey())){

                            if(groupPullData.getGroupRules()!=null&&!groupPullData.getGroupRules().isEmpty()){
                                effectuateGroupVoList=new ArrayList<>();
                                for (GroupRule groupRule:groupPullData.getGroupRules()) {
                                    EffectuateGroupVo effectuateGroupVo = new EffectuateGroupVo();
                                    effectuateGroupVo.setDeviceId(groupRule.getDeviceId());
                                    effectuateGroupVo.setDeviceUniqueId(groupRule.getDeviceUniqueId());
                                    effectuateGroupVo.setDeviceName(groupRule.getDeviceName());
                                    effectuateGroupVo.setDeviceHeadImageUrl(groupRule.getDeviceHeadImageUrl());
                                    if(groupRule.getGroupRuleInfos()!=null&&!groupRule.getGroupRuleInfos().isEmpty()){
                                        List<GroupRuleInfoVo> groupRuleInfoVoList=new ArrayList<>();
                                        for (GroupRuleInfo groupRuleInfo:groupRule.getGroupRuleInfos()) {
                                            GroupRuleInfoVo groupRuleInfoVo = new GroupRuleInfoVo();
                                            groupRuleInfoVo.setDeviceId(groupRule.getDeviceId());
                                            groupRuleInfoVo.setDeviceUniqueId(groupRule.getDeviceUniqueId());
                                            groupRuleInfoVo.setDeviceName(groupRule.getDeviceName());
                                            groupRuleInfoVo.setGroupId(groupRuleInfo.getGroupId());
                                            groupRuleInfoVo.setGroupWxid(groupRuleInfo.getGroupWxid());
                                            groupRuleInfoVo.setGroupNick(groupRuleInfo.getGroupNick());
                                            groupRuleInfoVo.setGenders(groupRuleInfo.getGenders());
                                            groupRuleInfoVo.setDeviceHeadImageUrl(groupRule.getDeviceHeadImageUrl());
                                            groupRuleInfoVo.setGroupMemberNum((Integer) redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.GROUP_MEMBER_COUNT.getKey(),groupRuleInfo.getGroupWxid())));
                                            groupRuleInfoVoList.add(groupRuleInfoVo);
                                        }
                                        effectuateGroupVo.setGroupRuleInfoVoList(groupRuleInfoVoList);
                                    }
                                    effectuateGroupVoList.add(effectuateGroupVo);
                                }

                            }
                        }
                    }
                }

                return effectuateGroupVoList;
            }
        }


        return null;
    }
}
